package lxt.dao.impl;

import java.util.ArrayList;
import java.util.List;

import lxt.dao.CostFixDao;
/**
 * 车辆维修管理dao
 * @author Lit
 *
 */
public class CostFixDaoImpl extends ABseDaoImpl implements CostFixDao {

	@SuppressWarnings("unchecked")
	public List<Object> loadAllCostFix(String truckId, String driverId,
			String fixStation, int offset, int length) {
		StringBuffer hql = new StringBuffer("Select tc,td.name from CostFix tc,DriverInfo td where tc.driverId = td.id ");
		ArrayList<Object> param = new ArrayList<Object>();
		if(truckId!=null&&!truckId.equals("")){
			hql.append(" And tc.truckId like ? ");
			param.add("%"+truckId+"%");
		}
		if(driverId!=null&&!driverId.equals("")){
			hql.append(" And td.name like ? ");
			param.add("%"+driverId+"%");
		}
		if(fixStation!=null&&!fixStation.equals("")){
			hql.append(" And tc.fixStation like ? ");
			param.add("%"+fixStation+"%");
		}
		
		return (List<Object>)this.queryForPage(hql.toString(), param, offset, length);
	}

	public int loadAllCostFixCount(String truckId, String driverId,
			String fixStation) {
		StringBuffer hql = new StringBuffer("Select tc,td.name from CostFix tc,DriverInfo td where tc.driverId = td.id ");
		ArrayList<Object> param = new ArrayList<Object>();
		if(truckId!=null&&!truckId.equals("")){
			hql.append(" And tc.truckId like ? ");
			param.add("%"+truckId+"%");
		}
		if(driverId!=null&&!driverId.equals("")){
			hql.append(" And td.name like ? ");
			param.add("%"+driverId+"%");
		}
		if(fixStation!=null&&!fixStation.equals("")){
			hql.append(" And tc.fixStation like ? ");
			param.add("%"+fixStation+"%");
		}
		
		return this.getAllRowCount(hql.toString(), param);
	}
	
	/**
	 * 根据车牌号查询车辆维修次数
	 * @param truckId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public int queryFixNum(String truckId){
		StringBuffer hql = new StringBuffer("Select count(*) from CostFix tc where 1=1 ");
		ArrayList<Object> param = new ArrayList<Object>();
		if(truckId!=null&&!truckId.equals("")){
			hql.append(" And tc.truckId = ? ");
			param.add(truckId);
		}
		List<Object> list = this.getHibernateTemplate().find(hql.toString(),param.toArray());
		Object o = (Object) list.get(0);
		Integer i = Integer.parseInt(o.toString());
		return i;
	}
}
